package com.bite.com.book;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/book")
public class BookController {
    @PostMapping("/getList")
    public List<BookInfo> getList() {
        List<BookInfo> bookInfos;
        // 此处先mock测试一下数据
        bookInfos = mock();

        // 判断借阅情况，对statusCN解释
        for (BookInfo bookInfo : bookInfos) {
            if (bookInfo.getStatus() == 1) bookInfo.setStatusCN("可借阅");
            else bookInfo.setStatusCN("不可借阅");
        }
        return bookInfos;
    }

    /**
     * 测试数据 假设是从数据库中拿取
     * @return 图书列表
     */
    private List<BookInfo> mock() {
        List<BookInfo> list = new ArrayList<>();
        for (int i = 1; i <= 15; i++) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setPrice(BigDecimal.valueOf(new Random().nextInt(100)));
            bookInfo.setBookId(i);
            bookInfo.setNum(new Random().nextInt(10));
            bookInfo.setBookName("图书：" + i);
            bookInfo.setAuthor("作者：" + i);
            bookInfo.setPublish("出版社：" + i);
            bookInfo.setStatus(i % 5 == 0 ? 1 : 2);// 1-可借阅 2-不可借阅
            list.add(bookInfo);
        }
        return list;
    }
}
